package com.exartisan.adapter;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.exartisan.bean.Event;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.DateTimeFormat;
import com.nostra13.universalimageloader.core.ImageLoader;

public class EventAdapter extends ArrayAdapter<Event> {
	private ArrayList<Event> listEvent;
	Context ctx;
	int row;
	Event event;
	private ArrayList<String> colors;

	public EventAdapter(Context context, int textViewResourceId,
			ArrayList<Event> objects) {
		super(context, textViewResourceId, objects);
		this.ctx = context;
		this.row = textViewResourceId;
		this.listEvent = objects;
		
		colors = new ArrayList<String>();
		colors.add("#ff5e4d");
		colors.add("#527423");
		colors.add("#5a5e6b");
		colors.add("#df6925");
		colors.add("#25bfda");
		colors.add("#9e0e40");
		Collections.shuffle(colors);
	}
	
	public void setEventData(ArrayList<Event> events){
		this.listEvent = events;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		event = listEvent.get(position);
		LayoutInflater inflate = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder holder;
		View row;
		if (convertView == null) {
			row = inflate.inflate(R.layout.row_list_event, null);
			holder = new ViewHolder();
			holder.txtTitle = (TextView) row.findViewById(R.id.txtTite_event);
			holder.txtPlace = (TextView) row.findViewById(R.id.txtPlace_event);
			holder.txtNum_View = (TextView) row.findViewById(R.id.txtNumView);
			holder.txtStart = (TextView) row.findViewById(R.id.txtTime_Start);
			holder.txtEnd = (TextView) row.findViewById(R.id.txtTime_End);
			holder.imgCover = (ImageView) row.findViewById(R.id.imgEvent);
			holder.line = row.findViewById(R.id.line);
			row.setTag(holder);
		} else { 
			row = (View)convertView;
			holder = (ViewHolder) row.getTag();
		}
		holder.txtTitle.setText(event.getEventName());
		holder.txtPlace.setText(event.getPlace());
		holder.txtNum_View.setText(event.getViewCount() + "");
		Log.d("startDay", event.getStartDate());
		if (event.getStartDate().equals("null")||event.getStartDate().length()<1||event.getEndDate().equals("null")||event.getEndDate().length()<1){
			row.findViewById(R.id.layoutTimeStart).setVisibility(View.GONE);
			row.findViewById(R.id.layoutTimeEnd).setVisibility(View.GONE);
		} else {
			row.findViewById(R.id.layoutTimeStart).setVisibility(View.VISIBLE);
			row.findViewById(R.id.layoutTimeEnd).setVisibility(View.VISIBLE);
			holder.txtStart.setText(DateTimeFormat.format(event.getStartDate(), DateTimeFormat.SHORT_VALUE));
			holder.txtEnd.setText(DateTimeFormat.format(event.getEndDate(), DateTimeFormat.SHORT_VALUE));
		}
		//holder.imgCover.setImageUrl(event.getThumbUrl());
		ImageLoader.getInstance().displayImage(event.getThumbUrl(),  holder.imgCover);
		holder.txtTitle.setTextColor(Color.parseColor(colors.get(position%colors.size())));
		holder.line.setBackgroundColor(Color.parseColor(colors.get(position%colors.size())));
		
		return row;
	}

	class ViewHolder {
		TextView txtTitle, txtPlace, txtNum_View, txtStart, txtEnd;
		ImageView imgCover;
		View line;
	}
}
